package com.boco.webmaster.common.taglib;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.struts.Globals;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.util.RequestUtils;

public class OnLoadTag extends TagSupport {
    protected String name = Globals.ERROR_KEY;

    public OnLoadTag() {
    }

    public int doStartTag() throws JspException {

        // Were any error messages specified?
        ActionErrors errors = null;
        try {
            errors = RequestUtils.getActionErrors(pageContext, name);
        } catch (JspException e) {
            RequestUtils.saveException(pageContext, e);
            throw e;
        }

        try {
            pageContext.getOut().write("<script>\n");

            pageContext.getOut().write("function onLoad(){\n");
            if ((errors != null) && !errors.isEmpty()) {
                pageContext.getOut().write("  showErrors();\n");
            }
            pageContext.getOut().write("}\n");
            pageContext.getOut().write("</script>\n");
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        return (EVAL_BODY_INCLUDE);
    }
}
